package com.airnewslinkosky.ticket.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.CabinItem;
import com.airnewslinkosky.ticket.model.FlightInfo;
import com.airnewslinkosky.ticket.model.SearchFlight;
import com.airnewslinkosky.ticket.ui.flight.FlyDetail;

public class FlyListViewAdapter extends SimpleAdapter  {
	LayoutInflater layoutInflater ;
	List<Map<String, Object>> items;
	Context context;
	List<FlightInfo> flist;
	SearchFlight searchFlight;
	int local;

	public FlyListViewAdapter(Context context, List<Map<String, Object>> items, int resource, String[] from, int[] to) {   
		super(context, items, resource, from, to);   
		this.context = context;
		layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
	}  
	
	public FlyListViewAdapter(Context context, List<Map<String, Object>> items,List<FlightInfo> flist,SearchFlight searchFlight) { 
		super(context, items, 0, null, null);  
		this.context = context;
		this.items = items;
		this.flist = flist;
		this.searchFlight = searchFlight;
		this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
	}  
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		  ViewHolder holder; 
		//得到自己定义的List的界面
		  if (convertView == null) { 
			  convertView = this.layoutInflater.inflate(R.layout.fly_row, null); 
			  holder = new ViewHolder(); 
			  holder.airline = (TextView) convertView.findViewById(R.id.airline); 
			  holder.departureTime = (TextView) convertView.findViewById(R.id.departureTime); 
			  holder.flightNo = (TextView) convertView.findViewById(R.id.flightNo); 
			  holder.arrival = (TextView) convertView.findViewById(R.id.arrival); 
			  holder.departure = (TextView) convertView.findViewById(R.id.departure); 
			  holder.airLogo = (ImageView) convertView.findViewById(R.id.airLogo); 
			  holder.baseCabin = (TextView) convertView.findViewById(R.id.baseCabin); 
			  holder.arrivalTime = (TextView) convertView.findViewById(R.id.arrivalTime); 
			  holder.ticketType = (TextView) convertView.findViewById(R.id.ticketType); 
			  holder.basePrice = (TextView) convertView.findViewById(R.id.basePrice); 
			  holder.cabinPrice = (TextView) convertView.findViewById(R.id.cabinPrice); 
			  holder.discount = (TextView) convertView.findViewById(R.id.discount); 
			  holder.seat = (TextView) convertView.findViewById(R.id.seat); 
			  
			  holder.longPressLayout = (LinearLayout) convertView.findViewById(R.id.longPressLayout);
			  
			  convertView.setTag(holder); 
		  } else {
			  holder = (ViewHolder) convertView.getTag(); 
		  }
		  
		  holder.airline.setText(String.valueOf(this.items.get(position).get("airline")));
		  holder.departureTime.setText(String.valueOf(this.items.get(position).get("departureTime")));
		  holder.flightNo.setText(String.valueOf(this.items.get(position).get("flightNo")));
		  holder.arrival.setText(String.valueOf(this.items.get(position).get("arrival")));
		  holder.departure.setText(String.valueOf(this.items.get(position).get("departure")));
		  holder.basePrice.setText(String.valueOf(this.items.get(position).get("basePrice")));
		  holder.baseCabin.setText(String.valueOf(this.items.get(position).get("baseCabin")));
		  holder.arrivalTime.setText(String.valueOf(this.items.get(position).get("arrivalTime")));
		  holder.ticketType.setText(String.valueOf(this.items.get(position).get("ticketType")));
		  holder.discount.setText(String.valueOf(this.items.get(position).get("discount")));
		  holder.cabinPrice.setText(String.valueOf(this.items.get(position).get("cabinPrice")));
		  holder.airLogo.setImageResource(Integer.valueOf(String.valueOf(this.items.get(position).get("airLogo"))));
		  
//		  holder.cabinPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
//		  holder.discount.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
		  
		  String seatValue = String.valueOf(this.items.get(position).get("seat"));
		  if(seatValue.indexOf(">9")!=-1) {
			  holder.seat.setVisibility(View.INVISIBLE);
		  } else {
			  holder.seat.setText("票少");
			  holder.seat.setVisibility(View.VISIBLE);
		  }
		  List<Map<String, Object>> childList = (List<Map<String, Object>>)this.items.get(position).get("cabinItemSmallList");
		  final FlightInfo fInfo = (FlightInfo)items.get(position).get("finfo");
		  if(childList != null && childList.size() > 0){
			  holder.longPressLayout.removeAllViews();
			  local = 0;
			  for(final Map<String, Object> child : childList){
				  holder.longPressLayout.addView(getChildView(child,fInfo));
			  }
		  }
		  
			
		  return convertView;

	}
	
	public View getChildView(final Map<String, Object> child,final FlightInfo fInfo) {  
		ViewHolder holder; 
		View childView = null;
		 //得到自己定义的List的界面
		  if (childView == null) { 
			  childView = this.layoutInflater.inflate(R.layout.fly_row_more, null); 
			  holder = new ViewHolder(); 
			  //更多的价钱项
			  holder.ticketTypeSmall = (TextView) childView.findViewById(R.id.ticketTypeSmall); 
			  holder.basePriceSmall = (TextView) childView.findViewById(R.id.basePriceSmall); 
			  holder.seatSmall = (TextView) childView.findViewById(R.id.seatSmall); 
			  holder.baseCabinSmall = (TextView) childView.findViewById(R.id.baseCabinSmall); 
			  holder.moreFlyLayout = (LinearLayout) childView.findViewById(R.id.moreFlyLayout);
			  holder.rebateSmall = (TextView) childView.findViewById(R.id.rebateSmall); 
			  holder.discountSmall = (TextView) childView.findViewById(R.id.discountSmall); 
			  childView.setTag(holder); 
		  } else {
			  holder = (ViewHolder) childView.getTag(); 
		  }
		  if(child!=null){
			  holder.ticketTypeSmall.setText(String.valueOf(child.get("ticketType")));
			  holder.basePriceSmall.setText(String.valueOf(child.get("cabinPrice")));
			  holder.baseCabinSmall.setText(String.valueOf(child.get("baseCabin")));
			  holder.rebateSmall.setText(String.valueOf(child.get("basePrice")));
			  holder.discountSmall.setText(String.valueOf(child.get("discount")));
			  String seatValue2 = String.valueOf(child.get("seat"));
			  if(seatValue2.indexOf(">9")!=-1) {
				  holder.seatSmall.setVisibility(View.INVISIBLE);
			  } else {
				  holder.seatSmall.setText("票少");
				  holder.seatSmall.setVisibility(View.VISIBLE);
			  }
			  
			  holder.moreFlyLayout.setOnClickListener(new OnClickListener(){
					public void onClick(View arg0) {
						CabinItem cabinItem= new CabinItem();
		            	cabinItem.price = ((String)child.get("price")).replace("净价:", "").replace("元", "");
		            	cabinItem.seat = (String)child.get("seat");
		            	cabinItem.cabinPrice = ((String)child.get("cabinPrice")).replace("票价:", "").replace("元", "");
		            	cabinItem.baseCabin = (String)child.get("baseCabinRealValue");
		            	cabinItem.discount = ((String)child.get("discount")).replace("(", "").replace(")", "");
		            	cabinItem.ibePayment = (String)child.get("ibePayment");
		            	cabinItem.airportTax = (String)child.get("airportTax");
		            	cabinItem.fuelTax = (String)child.get("fuelTax");
		            	cabinItem.ticketType = (String)child.get("ticketType");
		            	Intent intent = new Intent();
		    			intent.setClass(context, FlyDetail.class);
		    			FlyDetail.searchFlight = searchFlight;
		    			FlyDetail.flight = fInfo;
		    			FlyDetail.flight.cabinItemDetail = cabinItem;
		    			FlyDetail.flist = flist;
		    			context.startActivity(intent);
					}
			  });
		  }
		  return childView;
	}  
	
	static class ViewHolder { 
		LinearLayout longPressLayout,listViewLayout,moreFlyLayout;
		
		ListView flyMorelist;
		
		TextView airline,departureTime,flightNo; 
		ImageView airLogo;
		TextView arrival,departure,basePrice; 
		TextView baseCabin,arrivalTime,ticketType; 
		TextView cabinPrice,discount,seat; 
		
		TextView basePriceSmall,ticketTypeSmall,cabinPriceSmall,baseCabinSmall,seatSmall,rebateSmall,discountSmall; 
		
	} 


}
